//
//  SLabel.m
//  EasyRetail
//
//  Created by Samuel on 15/9/14.
//
//

#import "SLabel.h"

@implementation SLabel

-(BOOL)canBecomeFirstResponder

{
    
    return YES;
    
}
// 可以响应的方法

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{
    
    return (action == @selector(copy:));
    
}

//针对于响应方法的实现

-(void)copy:(id)sender

{
    
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    
    pboard.string = self.text;
    
}

//有了以上三个方法，我们就能处理copy了，当然，在能接收到事件的情况下：


-(void)setIsLongTap:(BOOL)isLongTap
{
    _isLongTap = isLongTap;
    self.userInteractionEnabled = YES;
    if (!self.isLongTap) {
        UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        touch.numberOfTapsRequired = 2;
        [self addGestureRecognizer:touch];
    }
    else
    {
        UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
        [self addGestureRecognizer:longTap];
    }
}
//UILabel默认是不接收事件的，我们需要自己添加touch事件

-(void)attachTapHandler
{
    
      //用户交互的总开关
    
    
    
    
    
    
    
//    [touch release];
    
}

//绑定事件

- (id)initWithFrame:(CGRect)frame

{
    
    self = [super initWithFrame:frame];
    
    if (self)
        
    {
        
//        [self attachTapHandler];
        
    }
    
    return self;
    
}

//同上

-(void)awakeFromNib

{
    
    [super awakeFromNib];
    
//    [self attachTapHandler];
    
}

//我们已经可以接收到事件了！由于我在上方将tap数设为2，所以需要双击才能捕获，
//
//接下来，我们需要处理这个tap，以便让菜单栏弹出来：

-(void)handleTap:(UIGestureRecognizer*) recognizer

{
    
    [self becomeFirstResponder];
    
    UIMenuItem *copyLink = [[UIMenuItem alloc]initWithTitle:@"复制"action:@selector(copy:)];
    
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];
    
}
- (void)longTap:(UILongPressGestureRecognizer*)sender
{
    [self becomeFirstResponder];
    UIMenuItem *copyLink = [[UIMenuItem alloc]initWithTitle:@"复制"action:@selector(copy:)];
    
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];
}

-(void) drawTextInRect:(CGRect)rect {
    self.topSpacing     = (self.topSpacing)?      self.topSpacing:    5;
    self.leftSpacing    = (self.leftSpacing)?     self.leftSpacing:   8;
    self.bottomSpacing  = (self.bottomSpacing)?   self.bottomSpacing: 5;
    self.rightSpacing   = (self.rightSpacing)?    self.rightSpacing:  8;
    
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect,
                                                       UIEdgeInsetsMake(self.topSpacing,
                                                                        self.leftSpacing,
                                                                        self.bottomSpacing,
                                                                        self.rightSpacing))];
}
@end
